package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.parkingwang.keyboard.KeyboardInputController;
import com.parkingwang.keyboard.OnInputChangedListener;
import com.parkingwang.keyboard.PopupKeyboard;
import com.parkingwang.keyboard.engine.KeyboardEntry;
import com.parkingwang.keyboard.view.InputView;
import com.parkingwang.keyboard.view.KeyboardView;
import com.parkingwang.keyboard.view.OnKeyboardChangedListener;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.CodeModel;
import com.pengcheng.taskapplication.model.PlateNoModel;
import com.pengcheng.taskapplication.model.UserDetailVo;
import com.pengcheng.taskapplication.model.VehicleId;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.view.fragment.DialogFragmentFromBottom;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/24.
 */

public class ApplyActivity extends BaseActivity implements IView,DialogFragmentFromBottom.OnFragmentInteractionListener {
    private UserPresenter userPresenter;
    private InputView mInputView;
    private PopupKeyboard mPopupKeyboard;
    private boolean mHideOKKey = false;
    @BindView(R.id.tv_apply)
    TextView tv_apply;
    @BindView(R.id.tv_plate)
    TextView tv_plate;
    private KeyboardView mKeyboardView;
    private KeyboardInputController mController;
    private Button lockTypeButton;
    private UserDetailVo userDetailVo;

    @Override
    protected int layoutResID() {
        return R.layout.activity_input_plateno_layout;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
//        initView();
//        getLogInfo();
        showDialog();
    }

    void showDialog() {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        DialogFragmentFromBottom newFragment = DialogFragmentFromBottom.newInstance();
        newFragment.show(ft, "dialog");
    }

    public void initKeyBorder(){
        // 使用 KeyboardInputController 来关联
        mInputView = findViewById(R.id.input_view);
        mController = KeyboardInputController
                .with(mKeyboardView, mInputView);
        mController.useDefaultMessageHandler();
    }
    public void initView(){
        mInputView = findViewById(R.id.input_view);
        lockTypeButton=findViewById(R.id.lockTypeButton);
        // 创建弹出键盘
        mPopupKeyboard = new PopupKeyboard(this);
        mKeyboardView=mPopupKeyboard.getKeyboardView();
        // 弹出键盘内部包含一个KeyboardView，在此绑定输入两者关联。
        mPopupKeyboard.attach(mInputView, this);

        // 隐藏确定按钮
        mPopupKeyboard.getKeyboardEngine().setHideOKKey(mHideOKKey);

        // KeyboardInputController提供一个默认实现的新能源车牌锁定按钮
        // KeyboardInputController提供一个默认实现的新能源车牌锁定按钮
        mPopupKeyboard.getController()
                .setDebugEnabled(true)
                .setSwitchVerify(true)
                .bindLockTypeProxy(new KeyboardInputController.ButtonProxyImpl(lockTypeButton) {
                    @Override
                    public void onNumberTypeChanged(boolean isNewEnergyType) {
                        super.onNumberTypeChanged(isNewEnergyType);
                        if (isNewEnergyType) {
                            lockTypeButton.setTextColor(getResources().getColor(android.R.color.holo_green_light));
                        } else {
                            lockTypeButton.setTextColor(getResources().getColor(android.R.color.black));
                        }
                    }
                });

        mPopupKeyboard.getController().addOnInputChangedListener(new OnInputChangedListener() {
            @Override
            public void onChanged(String number, boolean isCompleted) {
                if (isCompleted) {
                    mPopupKeyboard.dismiss(context);
                }
            }

            @Override
            public void onCompleted(String number, boolean isAutoCompleted) {
                mPopupKeyboard.dismiss(context);
            }
        });

    }
    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.isapply)){
           // 车辆ID，当vehicleId大于0是，该车牌已存在，需进行迁移流程
            VehicleId vehicleId=(VehicleId)data;
            if(vehicleId.getVehicleId()>0){

            }else{
                PlateNoModel model=new PlateNoModel();
                model.setPlateNo(tv_plate.getText().toString());
                apply(model);
            }
        }
        if(actionType.equals(ConstantValue.RequestKey.apply)){
            //工单号
            CodeModel model=(CodeModel) data;
            Intent intent =new Intent(context,ApplySuccessActivity.class);
            intent.putExtra("code",model.getCode());
            startActivity(intent);
            ApplyActivity.this.finish();
            //然后跳转到信息预填写页面
        }
        if(actionType.equals(ConstantValue.RequestKey.userinfo)){
           //用户信息
            userDetailVo=(UserDetailVo)data;
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }
    @Override
    protected void onResume() {
        super.onResume();
        // 默认选中第一个车牌号码输入框
  //      mInputView.performFirstFieldView();
    }

    @OnClick({R.id.tv_apply,R.id.img_back,R.id.tv_plate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_apply:
                    String plateno=tv_plate.getText().toString().trim();
                    if(plateno==null||plateno.length()==0){
                        ToastUtils.showShortToast("请输入正确的车牌号");
                        return;
                    }
                    isapply(plateno);
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_plate:
                showDialog();
                break;
        }
    }
    private void apply(PlateNoModel plateNo){
        userPresenter.apply(plateNo, ConstantValue.RequestKey.apply);
    }

    private void isapply(String plateNo){
        userPresenter.isapply(plateNo, ConstantValue.RequestKey.isapply);
    }
    private void getLogInfo(){
        userPresenter.getUserDetail(ConstantValue.RequestKey.userinfo);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onDelete() {

    }

    @Override
    public void onCompete(String plateNum) {
        tv_plate.setText(plateNum);
    }
}
